<template>
  <div>
    <PanelHead/>
  </div>
  <!-- 页面数据展示部分   -->
  <el-table :data="tableData.list" style="width: 100%;">
    <el-table-column prop="id" label="id" />
    <el-table-column prop="name" label="昵称" />
    <el-table-column prop="permissions_id" label="所属组别">
      <template #default="scope">
        <span>{{ permissionName(scope.row.permissions_id) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="电话号码" />
    <el-table-column prop="active" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.active ? 'success' : 'danger'">{{ scope.row.active ? '正常' : '失效' }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="item.create_time" label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon style=" margin-right: 5px;">
            <Clock />
          </el-icon>
          <span>{{ scope.row.create_time }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <!-- <el-button type="text" @click="edit(scope.row)"  @click="del(scope.row)" @click="open(scope.row)">编辑</el-button> -->
        <el-button @click="open(scope.row)" type="primary">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页部分 -->
  <div class="pagination-info">
    <el-pagination v-model:current-page="paginationData.pageNum" :page-size="paginationData.pageSize"
      :background="false" size="small" layout="total, prev, pager, next" :total="tableData.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>

  <!-- 弹窗部分 -->
  <el-dialog v-model="dialogFormVisible" :before-close="beforeClose" title="添加权限" width="500">
    <el-form 
      ref="formRef" 
      label-width="100px" 
      label-position="left" 
      :model="form" 
      :rules="rules"
    >
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile"  disabled />
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select v-model="form.permissions_id" placeholder="请选择菜单权限" style="width: 240px;">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>

    <el-button class="button" type="primary" @click="confirm(formRef)">确认</el-button>
  </el-dialog>


</template>

<script setup lang="ts">
import { getAdmin, menuSelectList, updateUser } from '@/api/index';
import { ref, reactive, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import dayjs from 'dayjs'


const route = useRoute()
console.log(route, 'route-xx')

// 页面数据展示部分
// 分页的两个函数
// 页码
const handleSizeChange = (val: number) => {
  paginationData.pageSize = val
}
// 当前页         
const handleCurrentChange = (val: number) => {
  paginationData.pageNum = val
}

const paginationData = reactive({
  pageNum: 1,
  pageSize: 10
});

const tableData = reactive({
  list: [],
  total: 0,
})
// 列表数据
onMounted(() => {
  getListData()
  menuSelectList().then(({ data }) => {
    console.log(data.data, 'menuSelectList 列表下拉')
    options.value = data.data
  })
})
const options = ref([])
// 根据权限id去匹配权限名称
const permissionName = (id: number) => {
  const data = options.value.find(el => el.id === id)
  return data ? data.name : '超级管理员'
}

// 请求列表
const getListData = () => {
  getAdmin(paginationData).then(({ data }) => {
    console.log(data, 'getAdmin')
    const { list, total } = data
    list.forEach(item => {
      item.create_time = dayjs(list.create_time).format('YYYY-MM-DD')
    });
    tableData.list = list
    tableData.total = total
  })
}

// 编辑按钮
const open = (rowData)=>{
  dialogFormVisible.value = true 
  Object.assign(form,{mobile: rowData.mobile, name: rowData.name, permissions_id: rowData.permissions_id, })
}

// 弹窗的所用到的数据



// 控制弹窗的隐藏和显示
const dialogFormVisible = ref(false)

// 关闭弹窗前的回调函数
const beforeClose = () => {
  dialogFormVisible.value = false
}

// 表单实例
const formRef = ref()

// 表单数据
const form = reactive({
  mobile:'',
  name: '',
  permissions_id: '',
})

// 表单验证规则
const rules = reactive({
  name: [
    { required: true, message: '请输入权限名称', trigger: 'blur' },
  ],
  permissions_id: [
    { required: true, message: '请选择菜单权限', trigger: 'blur' },
  ],

});

// 提交表单
const confirm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const { name, permissions_id } = form
      updateUser({name, permissions_id}).then(({data}) => {
        console.log(data, 'updateUser')
        if(data.code === 10000){
          dialogFormVisible.value = false
          getListData()
        }
      })

    } else {
      console.log('error submit!', fields);
    }
  })
}
</script>

<style scoped>
.flex-box {
  display: flex;
  align-items: center;

}

.pagination-info {
  margin-top: 10px;
  float: right;
}

.button {
  /* float: right; */
  margin-top: 10px;
}
</style>